const tasklist = require('../models/model/taskList');


/**
 * 创建任务
 * @param {任务名称} name     string
 * @param {描述} description  string
 * @param {内容} content   string
 * @param {目标完成时间} completeTargetTime  date
 * @param {完成时间} completeTime  date
 * @param {是否完成} isDone boolean
 * @param {用户Id} userId  string
 * @returns 返回创建的实例
 */
exports.createTaskList = async (name = "", description = "", content = "", completeTargetTime = null, completeTime = null, isDone = 0, userId = '') => {
    const ins = await tasklist.create({
        name,
        description,
        content,
        completeTargetTime,
        completeTime,
        isDone,
        userId
    });
    if (ins) {
        return ins.toJSON();
    }
    return null;
}



/**
 * 
 * @param {第几页} page  number
 * @param {一个几条数据} limit  number
 * @param {用户Id} userId  string
 * @returns 
 */
exports.findTaskList = async (page = 1, limit = 10, userId = '') => {
    const ins = await tasklist.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: {
            userId
        }

    });
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 
 * @param {任务id根据id删除任务}} id 
 */
exports.deleteTask = async (id) => {
    const ins = await tasklist.destroy({
        where: {
            id
        }
    });
    return ins;
}


/**
 * 更新任务
 * @param {任务id} id 
 * @param {任务名} name 
 * @param {描述} description 
 * @param {内容} content 
 * @param {预计完成时间} completeTargetTime 
 */
exports.updateTask = async (id, name, description, content, completeTargetTime) => {
    return await tasklist.update(
        {
            name,
            description,
            content,
            completeTargetTime
        },
        {
            where: {
                id
            }
        }
    );
}

/**
 * 根据id完成任务
 * @param {任务id} id 
 */
exports.doneTask = async (id) => {
    return await tasklist.update(
        {
            isDone: true
        },
        {
            where: {
                id
            }
        }
    )
}